#include <stdio.h>
#include <SDL2/SDL.h>
#include <stdlib.h>
#define W 480;
#define H 320;

int main(int argc, char *argv[])
{
    SDL_Window *window = NULL;
    int quit = 1;
    SDL_Renderer *render;
    SDL_Texture *texture;
    SDL_Rect rect;
    SDL_Init(SDL_INIT_VIDEO);
    int w = W;
    int h = H;
    window = SDL_CreateWindow("YUANXUZHEN",
                              100,
                              100,
                              w + 100,
                              h + 100,
                              SDL_WINDOW_SHOWN);

    if (!window)
    {
        printf("create window fail\n");
        goto __EXIT;
    }

    
    render = SDL_CreateRenderer(window, -1, 0);
    if (!render)
    {
        printf("create render fail\n");
        goto __EXIT;
    }


    
    texture = SDL_CreateTexture(render,
                                SDL_PIXELFORMAT_ABGR8888,
                                SDL_TEXTUREACCESS_TARGET,
                                w,
                                h);
    if (!texture)
    {
        printf("create texture fail\n");
        goto __EXIT;
    }

    do
    {
        SDL_Event event;
        SDL_WaitEvent(&event);
        switch (event.type)
        {
        case SDL_QUIT:
            quit = 0;
            break;

        default:
            printf("EVENT TYPE %d\n", quit);
            break;
        }

        rect.w = 20;
        rect.h = 20;
        rect.x = rand() % w;
        rect.y = rand() % h;

        SDL_SetRenderTarget(render, texture);
        SDL_SetRenderDrawColor(render, 0, 0, 0, 255);
        SDL_RenderClear(render);

        SDL_RenderDrawRect(render, &rect);
        SDL_SetRenderDrawColor(render, 255, 0, 0, 255);
        SDL_RenderFillRect(render, &rect);

        SDL_SetRenderTarget(render, NULL);
        SDL_RenderCopy(render, texture, NULL, NULL);
        SDL_RenderPresent(render);


    } while (quit);
__EXIT:
    if (render)
    {
        SDL_DestroyRenderer(render);
    }

    if (texture)
    {
        SDL_DestroyTexture(texture);
    }

    if (window)
    {
        SDL_DestroyWindow(window);
    }

    SDL_Quit();
    return 0;
}